package cn.dwy2002.binaryhandwritingserver.controller;

import cn.dwy2002.binaryhandwritingserver.common.Result;
import cn.dwy2002.binaryhandwritingserver.exception.ProjectNotFoundException;
import cn.dwy2002.binaryhandwritingserver.exception.ServiceException;
import cn.dwy2002.binaryhandwritingserver.pojo.dto.ProjectDTO;
import cn.dwy2002.binaryhandwritingserver.pojo.vo.ProjectVO;
import cn.dwy2002.binaryhandwritingserver.service.service.ProjectService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 项目管理
 *
 * @author 杜伟毅
 * @version 1.0
 * @since 2025/05/31
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/projects")
public class ProjectController {
    private final ProjectService projectService;

    /**
     * 获取所有项目列表
     *
     * @return 项目列表
     * @throws ServiceException 查询失败时抛出
     */
    @GetMapping
    public Result<List<ProjectVO>> listAllProjects() throws ServiceException, ServiceException {
        return projectService.listAllProjects();
    }

    /**
     * 获取项目详情
     *
     * @param id 项目ID
     * @return 项目详情
     * @throws ProjectNotFoundException 项目不存在时抛出
     * @throws ServiceException         查询失败时抛出
     */
    @GetMapping("/{id}")
    public Result<ProjectVO> getProjectDetail(@PathVariable Long id)
            throws ProjectNotFoundException, ServiceException {
        return projectService.getProjectDetail(id);
    }

    /**
     * 创建新项目
     *
     * @param dto 项目数据
     * @return 创建结果
     */
    @PostMapping
    public Result<Void> createProject(@RequestBody ProjectDTO dto) {
        return projectService.createProject(dto);
    }

    /**
     * 更新项目信息
     *
     * @param dto 项目数据
     * @return 更新结果
     * @throws ProjectNotFoundException 项目不存在时抛出
     */
    @PutMapping
    public Result<Void> updateProject(@RequestBody ProjectDTO dto) throws ProjectNotFoundException {
        return projectService.updateProject(dto);
    }

    /**
     * 删除项目
     *
     * @param id 项目ID
     * @return 删除结果
     * @throws ProjectNotFoundException 项目不存在时抛出
     */
    @DeleteMapping("/{id}")
    public Result<Void> deleteProject(@PathVariable Long id) throws ProjectNotFoundException {
        return projectService.deleteProject(id);
    }
}
